import { Node, Vec2, Vec3 } from "cc";
import { GlobalEventManager } from "./GlobalEventManager";
import { PropType } from "../constants/GameConstants";

/**
 * 游戏事件类型定义
 */
export interface GameEvents {
    // UI事件
    'ui:head': { worldPos: Node, dstPos : Vec2, radius: number, goldCount: number, addGold: number, callBack?:Function};
    'ui:menu-toggle': { isOpen: boolean };
    'ui:dialog-show': { dialogId: string; data?: any };
    'ui:award': { active: boolean };
    'ui:prop:refresh': { prop: PropType}
    
    // 玩家事件
    'player:move': { position: { x: number; y: number; z: number }; direction: number };
    'player:jump': { height: number };
    'player:attack': { targetId?: string; damage: number };
    'player:damage': { damage: number; from: string };
    'player:death': { killerId?: string };
    
    // 游戏状态事件
    'game:start': { level: number };
    'game:pause': { isPaused: boolean };
    'game:over': { score: number; reason: string };
    'game:level-up': { newLevel: number; previousLevel: number };
    
    // 网络事件
    'network:connect': { status: 'success' | 'failure'; message?: string };
    'network:disconnect': { reason: string };
    'network:message': { type: string; data: any };
    
    // 音频事件
    'audio:play': { soundId: string; volume?: number };
    'audio:stop': { soundId: string };
    'audio:volume-change': { volume: number };
}

/**
 * 类型安全的全局事件管理器
 */
export class TypedEventManager {
    private static instance: TypedEventManager;
    private eventManager: GlobalEventManager;

    private constructor() {
        this.eventManager = GlobalEventManager.getInstance();
    }

    public static getInstance(): TypedEventManager {
        if (!TypedEventManager.instance) {
            TypedEventManager.instance = new TypedEventManager();
        }
        return TypedEventManager.instance;
    }

    /**
     * 监听事件（类型安全）
     */
    on<K extends keyof GameEvents>(
        event: K, 
        callback: (data: GameEvents[K]) => void, 
        target?: any
    ): void {
        this.eventManager.on(event, callback, target);
    }

    /**
     * 监听一次性事件（类型安全）
     */
    once<K extends keyof GameEvents>(
        event: K, 
        callback: (data: GameEvents[K]) => void, 
        target?: any
    ): void {
        this.eventManager.once(event, callback, target);
    }

    /**
     * 派发事件（类型安全）
     */
    emit<K extends keyof GameEvents>(event: K, data?: GameEvents[K]): void {
        this.eventManager.emit(event, data);
    }

    /**
     * 移除事件监听
     */
    // off<K extends keyof GameEvents>(
    //     event: K, 
    //     callback?: (data: GameEvents[K]) => void, 
    //     target?: any
    // ): void {
    //     this.eventManager.off(event, callback, target);
    // }

    /**
     * 移除所有监听
     */
    removeAll(target: any): void {
        this.eventManager.removeAll(target);
    }

    /**
     * 检查是否有监听器
     */
    hasListener<K extends keyof GameEvents>(event: K): boolean {
        return this.eventManager.hasListener(event);
    }
}

// 导出类型安全的事件管理器实例
export const eventManager = TypedEventManager.getInstance();